let audio = new Audio();
// import $request from '../util/request';
// 引入 $store,用于后续操作
import $store from "../store/index"
console.log("vuex实例", $store);



audio.playSong = async function (id) {
    $store.dispatch("get_song_detail", id);

    audio.src = `https://music.163.com/song/media/outer/url?id=${id}.mp3`;
    audio.oncanplay = function () {
        audio.play();
    }

};

// 将音频播放状态和相关信息存至 vuex 中，方便多页面使用
audio.addEventListener("playing", function () {
    // console.log("播放中");
    $store.commit('chang_state', true)
    console.log("获取歌曲", audio.duration);

})
audio.addEventListener("pause", function () {
    // console.log("暂停");
    $store.commit('chang_state', false)
})

audio.addEventListener("timeupdate", () => {
    let obj = {
        current_time: parseInt(audio.currentTime),
        duration: parseInt(audio.duration),
    };

    $store.commit("change_current_song", obj);
})
export default audio;